package com.sf.rbac.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sf.rbac.domain.Employee;
import com.sf.rbac.domain.Role;
import com.sf.rbac.ex.BusinessException;
import com.sf.rbac.mapper.EmployeeMapper;
import com.sf.rbac.mapper.RoleMapper;
import com.sf.rbac.qo.QueryObject;
import com.sf.rbac.service.IRoleService;
import com.sf.rbac.util.PageResult;
import com.sf.rbac.vo.RoleVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.nio.BufferOverflowException;
import java.util.List;

@Service
public class RoleServiceImpl implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public PageInfo<Role> query(QueryObject queryObject) {
        //告诉pageHelper  当前页和页容量
        PageHelper.startPage(queryObject.getPageNum(), queryObject.getPageSize());
        //调用selectForList查询方法
        List<Role> roles = roleMapper.selectForList();
        return new PageInfo<Role>(roles);
    }

    @Override
    public void delete(Long id) {
        //根据id删除数据
        int count = roleMapper.delete(id);
        //判断受影响行row 是否为0
        if (count == 0) {
            throw new BusinessException("非法操作");
        }
        //删除角色与用户的关系 employee_role
        roleMapper.deleteRoleAndEmpRelationByRId(id);
        //删除角色与权限的关系  role_permission
        roleMapper.deleteRoleAndPermissionByRId(id);
    }

    @Override
    public Role get(Long id) {
        //根据id 查询角色数据
        Role role = roleMapper.get(id);
        //查询数据是否为空
        if (role == null) {
            throw new BusinessException("非法操作");
        }
        return role;
    }

    @Override
    public List<Role> listAll() {
        return roleMapper.listAll();
    }

    @Override
    public List<Role> queryRolesByEmpId(Long id) {
        //id为员工id       根据员工id查询员工信息
        Employee employee = employeeMapper.get(id);
        //判断员工信息是否为空
        if (employee == null) {
            throw new BusinessException("非法操作");
        }
        //根据员工id 查询员工所有的角色信息
        List<Role> list = roleMapper.queryRolesByEmpId(id);
        return list;
    }

    @Override
    public void saveOrUpdate(RoleVo roleVo) {
        //1 vo获取role 获取permissionIds
        Role role = roleVo.getRole();
        Long[] permissionIds = roleVo.getPermissionIds();
        //2判断id 是否为空
        if (role.getId() == null) {
            //3 如果为空 是新增
            //3.1 新增角色信息
            roleMapper.insert(role);
        } else {
            //4 如果不为空 ,编辑
            //4.1 编辑角色的信息
            roleMapper.update(role);
            //4.2 删除旧关系
            roleMapper.deleteRoleAndPermissionByRId(role.getId());
        }
        //3.2 新增关系  role_permission
        //4.3 插入新关系
        if (permissionIds != null && permissionIds.length > 0) {
            for (Long permissionId : permissionIds) {
                roleMapper.insertRoleAndPermissionRelation(role.getId(), permissionId);
            }
        }

    }
}
